package cn.dengta.webapp.user.form.query;

import java.util.Arrays;

import cn.dengta.common.model.Bean;
import cn.dengta.common.util.Common;
import cn.dengta.context.model.Language;
import cn.dengta.context.model.WesKit;
import cn.dengta.webapp.base.form.BaseForm;
import cn.dengta.webapp.user.entity.Link;
import cn.dengta.webapp.user.model.LinkType;
import lombok.*;
import me.codeplayer.util.StringUtil;

@Getter
@Setter
public class LinkQuery extends BaseForm<Link> {

	public Long id;
	/** 名称 */
	public String name;
	/** 语言 */
	public Language language;
	/** 别名 */
	public String alias;
	/** 是否开放 */
	public Integer openStatus;
	@Setter(AccessLevel.NONE)
	public LinkType type;
	/** 类型多个逗号区分 */
	public String types;
	@Setter(AccessLevel.NONE)
	public LinkType[] linkTypes;
	/** 分包 */
	private WesKit wesKit;

	public void setTypes(String types) { // 前端多个获取
		if (StringUtil.notEmpty(types)) {
			final String[] split = types.split(Common.SEP);
			this.linkTypes = Arrays.stream(split).map(LinkType::getValueByName).toArray(LinkType[]::new);
		}
	}

	public void setType(String type) {
		this.type = LinkType.getValueByName(type);
	}

	public boolean contains(Link link) {
		if (link == null) {
			return Bean.isValidId(id) || StringUtil.notEmpty(name) || StringUtil.notEmpty(alias);
		}
		return link.getId().equals(id) || link.getName().equals(name) || link.getAlias().equals(alias);
	}

	public void setLanguage(Integer language) {
		this.language = Language.of(language);
	}

	public void setWesKit(Integer wesKit) {
		this.wesKit = WesKit.of(wesKit);
	}

}
